package pl.prv.polanie.client.dialogs;

import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.dto.CSSubjectDTO;
import pl.prv.polanie.client.dto.CSSubjectNameDTO;
import pl.prv.polanie.client.dto.CSSubjectTypeDTO;
import pl.prv.polanie.client.dto.CSVSubjectNameDTO;
import pl.prv.polanie.client.dto.CSVSubjectTypeDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.model.ViewDataProvider;
import pl.prv.polanie.client.panels.lists.SubjectsList;
import pl.prv.polanie.client.widgets.CustomDialogBox;
import pl.prv.polanie.client.widgets.SelectBoxWithObjects;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author Janusz Prokulewicz
 * Created: Apr 19, 2008
 */

public class CreateSubjectDialog extends CustomDialogBox {

	private static final String STYLE__CREATE_SUBJECT_EVENT_DIALOG = "createSubjectEventDialog";
	
	
	/** Podzial, dla ktorego jest tworzony nowy/edytowany istniejacy subjectEvent.*/
	private SubjectsList subjectsList = null;
	
	
	
	
	/***/
	private FlexTable widgetsFT = new FlexTable();
	
	private Label subjectNameL = new Label("Subject name");
	private SelectBoxWithObjects subjectNameSB = new SelectBoxWithObjects();
	
	
	private Label subjectTypeL = new Label("Subject type");
	private SelectBoxWithObjects subjectTypeSB = new SelectBoxWithObjects();
	

	
	private Button loadDataB = new Button("load dataa");

	private Button saveOrCreateAndCloseB = new Button("save and close");
	private Button saveOrCreateB = new Button("save/create");
	private Button closeB = new Button("close");
	
	
	private Command cmdAfterSaveButtonClicked = null;
	
	
	//referencja do dto edytowanego se
	private CSSubjectDTO csSubjectDTO = null;
	
//	public CreateSubjectDialog() {
//		this(null);
//	}
	
	
	public CreateSubjectDialog(SubjectsList subjectsList, CSSubjectDTO csL) {
		this.subjectsList = subjectsList;
		this.csSubjectDTO = csL;
		
		layoutWidgets();
		setupWidgets();

		
		if (csL != null) {
			setText("Edit subject");
		} else {
			setText("Create new subject");
		}

		setWidget(widgetsFT);
		//setStyleName(STYLE__CREATE_SUBJECT_EVENT_DIALOG);
		loadDataToWidgets();
	}



	private void setupWidgets() {
		
		//ACADEMIC
		subjectNameSB.addChangeListener(new ChangeListener() {
			public void onChange(Widget sender) {
				Planner.log("CreateSubjectDialog, acacdemicTitleLB, id => " + subjectNameSB.getSelectedObject());
			}
		});
		subjectNameSB.setWidth(300+"px");
		subjectNameSB.setItemsPanelWidth(400);
		subjectNameSB.setVisibleItemsCount(15);
		
		subjectTypeSB.setWidth(130+"px");
		subjectTypeSB.setItemsPanelWidth(200);
		

		
		saveOrCreateB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				boolean closeWindowAfterSaving = false;
				saveCSSubject(closeWindowAfterSaving);
			}
		});
		
		saveOrCreateAndCloseB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				boolean closeWindowAfterSaving = true;
				saveCSSubject(closeWindowAfterSaving);
			}
		});
		
		closeB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				hide();
			}
		});
		
		
		loadDataB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				loadDataToWidgets();
			}


		});
	}



	private void layoutWidgets() {
		widgetsFT.setWidget(0, 0, subjectNameL);
		widgetsFT.setWidget(0, 1, subjectNameSB);
		
		widgetsFT.setWidget(1, 0, subjectTypeL);
		widgetsFT.setWidget(1, 1, subjectTypeSB);
		
		widgetsFT.setWidget(7, 0, loadDataB);
		widgetsFT.setWidget(7, 1, saveOrCreateAndCloseB);
		
		widgetsFT.setWidget(8, 0, saveOrCreateB);
		widgetsFT.setWidget(8, 1, closeB);
		
	}
	
	/**
	 * TODO metoda validujaca dane
	 */
	private boolean validateEnteredData() {
		return true;
	}
	

	
	/**
	 * Wczytuje z cache'a dane do kontrolek. W przypadku edycji
	 * istniejacego SE ustawiane sa takze w kontrolkach odpowiednie wartosci.
	 */
	private void loadDataToWidgets() {

		long subjectNameID = -1;
		long subjectTypeID = -1;
		
		if (csSubjectDTO != null) {
			
			Planner.log("edycja Subject");
			
			subjectNameID = csSubjectDTO.getSubjectNameID();
			subjectTypeID = csSubjectDTO.getSubjectTypeID();
			
		} else {
			Planner.log("tworzenie Subjecta");
		}

		CSSubjectNameDTO[] subjectNames = Cache.getAllSubjectNames();
		subjectNameSB.clear();
		
		for (int i = 0; i < subjectNames.length; i++) {
			CSVSubjectNameDTO csvSN = ViewDataProvider.getCSVSubjectName(subjectNames[i]);
			subjectNameSB.addItem(csvSN.toLabel(), subjectNames[i]);
			
			if (subjectNameID == csvSN.getId()) {
				subjectNameSB.setSelectedIndex(i);
			}
		}
		subjectNameSB.sortWithAlphabeticalOrder();
		

		

		
		CSSubjectTypeDTO[] subjectTypes = Cache.getAllSubjectTypes();
		subjectTypeSB.clear();
		
		for (int i = 0; i < subjectTypes.length; i++) {
			CSVSubjectTypeDTO csvST = ViewDataProvider.getCSVSubjectType(subjectTypes[i]);
			subjectTypeSB.addItem(csvST.toLabel(), subjectTypes[i]);
			
			if (subjectTypeID == csvST.getId()) {
				subjectTypeSB.setSelectedIndex(i);
			}
		}
		subjectTypeSB.sortWithAlphabeticalOrder();
		
	}

	/**
	 * Zapisuje/aktualizuje subjectType. 
	 * @param closeWindowAfterSaving
	 */
	private void saveCSSubject(final boolean closeWindowAfterSaving) {
		if (validateEnteredData()) {
			final CSSubjectDTO csS = new CSSubjectDTO();
			
			//w przypadku edycji SE ustawiamy id
			if (csSubjectDTO != null) {
				csS.setId(csSubjectDTO.getId());
			}
			
			
			//ustawienie atrybutow w dto
			long subjectNameID = ((CSSubjectNameDTO)subjectNameSB.getSelectedObject()).getId();
			csS.setSubjectNameID(subjectNameID);
			
			long subjectTypeID = ((CSSubjectTypeDTO)subjectTypeSB.getSelectedObject()).getId();
			csS.setSubjectTypeID(subjectTypeID);
			
			
			
			
			
			AsyncCallback<Long> callback = new AsyncCallback<Long>() {
				public void onFailure(Throwable caught) {
					Planner.log("CreateSubjectDialog.saveCSSubject() FAILURE");
				}
				public void onSuccess(Long res) {
					Planner.log("CreateSubjectDialog.saveCSSubject() SUCCESS -> zwrocone ID = " + res);

					
					if (res.longValue() != 0) {
						//update w cache
						csS.setId(res.longValue());
						Cache.saveOrUpdateCSSubject(csS);
						
						//TODO dodac do kontrolki tabelki
						if (subjectsList != null) {
							subjectsList.addCSsubject(csS);
						}
					}

					if (closeWindowAfterSaving) {
						hide();
					}
					
				}
			};
			
			ServiceHolder.get().saveCSSubject(csS, callback);
		}
	}


	public void setCmdAfterSaveButtonClicked(Command cmdAfterSaveButtonClicked) {
		this.cmdAfterSaveButtonClicked = cmdAfterSaveButtonClicked;
	}

}
